<?php
namespace Home\Biz;
use Home\Biz\StringBiz;

class StyleBiz{
	
	public static function caculateTotalPage($totalCount, $pageSize)
	{
	
		$count = intval($totalCount / $pageSize);
	
		$countLeft = $totalCount % $pageSize;
	
		if ($countLeft != 0) {
			$count = $count + 1;
		}
	
		return $count;
	}
	
	public static function addStyle($style_sn,  $style_name, $style_brief ,
				$sku_channel, $sku_channel_name, $sale_date, $market_price , $shop_price,
				$sale_min_price , $sale_max_price , $sku_stock , $is_real ,
				$status){
				$style_id = StyleBiz::generateStyleId();
				$date = strtotime($sale_date);
				$add_style_en = array(
						'style_id'	=>$style_id,
						'style_sn'	=> $style_sn,
						'style_name'	=> $style_name,
						'style_brief'	=> $style_brief,
						'sku_channel'	=> $sku_channel ,
						'sku_channel_name'	=> $sku_channel_name,
						'sale_date'	=> date("y-m-d H:i:s", $date),
						'market_price'	=> $market_price,
						'shop_price'	=> $shop_price ,
						'sale_min_price'	=> $sale_min_price,
						'sale_max_price'	=> $sale_max_price ,
						'sku_stock'	=> $sku_stock ,
						'is_real'	=> $is_real ,
						'status'	=> $status ,
						'create_user'	=> "whaleyFz.product styleBiz.addStyle",
						'create_date'	=> date("y-m-d H:i:s", time())
				);
				$result = M('style')->add($add_style_en);
				
	}
	
	
	public static function updateStyle($style_id,$style_sn,  $style_name, $style_brief ,
			$sku_channel, $sku_channel_name, $sale_date, $market_price , $shop_price,
			$sale_min_price , $sale_max_price , $sku_stock , $is_real ,
			$status){				
				$date = strtotime($sale_date);
				$update_style_en = array(
						
						'style_sn'	=> $style_sn,
						'style_name'	=> $style_name,
						'style_brief'	=> $style_brief,
						'sku_channel'	=> $sku_channel ,
						'sku_channel_name'	=> $sku_channel_name,
						'sale_date'	=> date("y-m-d H:i:s", $date),
						'market_price'	=> $market_price,
						'shop_price'	=> $shop_price ,
						'sale_min_price'	=> $sale_min_price,
						'sale_max_price'	=> $sale_max_price ,
						'sku_stock'	=> $sku_stock ,
						'is_real'	=> $is_real ,
						'status'	=> $status ,
						'update_user'	=> "whaleyFz.product styleBiz.updateStyle",
						'update_date'	=> date("y-m-d H:i:s", time())
				);
				$result = M('style')->where('style_id ="%s"',$style_id)->save($update_style_en);
	
	}
	
	public static function queryStyleByPara($style_sn,  $style_name, $sku_channel,$pageSize,$index){
				
		if (!empty ($style_sn)) {
			$condition_array['style_sn'] = array('like', "%" . $style_sn . "%");
		}
		
		if (!empty ($style_name)) {
			$condition_array['style_name'] = array('like', "%" . $style_name . "%");
		}
		
		if (!empty ($sku_channel)) {
			$condition_array['sku_channel'] = array('like', "%" . $sku_channel . "%");
		}
		
				
				$lst_styles = M('style')->field()->where($condition_array)->page($index,$pageSize)->select();
			
				foreach ($lst_styles as $key => $lst_style){
						
					$lst_styles[$key]['sku_channel_text']=StringBiz::getSkuChannelRevenueStatusText($lst_style['sku_channel']);
					$lst_styles[$key]['status_text'] = StringBiz::getSkuStatusRevenueStatusText($lst_style['status']);
				}
				
				
				
				// 总记录数
				$totalCount = M('style')->where($condition_array)->count();
				
				// 总页数
				$totalPage = self::caculateTotalPage($totalCount, $pageSize);
				// 当前页记录数
				$currentCount = $lst_styles . $pageSize;
				// 下一页页码
				$nextIndex = $index + 1;
				// 上一页页码 如果是第一页，页码仍然为1
				$preIndex = 1;
				if ($index > 1) {
					$preIndex = $index - 1;
				}
				
				$resultMap = array(
						'totalCount' => $totalCount,
						'totalPage' => $totalPage,
						'currentPage' => $currentCount,
						'nextIndex' => $nextIndex,
						'preIndex' => $preIndex,
						'index' => $index,
						'datas' => $lst_styles
				);
				
				
				
				return $resultMap;
	
	}
	
	
	
	
	public static function generateStyleId(){
		$seq = 10000 + CommonBiz::getNewSeq('style_id');
		return 'ST'.$seq;
	}
	
	
}